package com.yyk.yiliao.ui.message;


import android.Manifest;
import android.os.Bundle;
import androidx.annotation.Nullable;

import android.view.View;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.orhanobut.logger.Logger;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yyk.yiliao.cache.UserCacheManager;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 环信会话内容
 */
public class ChatFragment extends EaseChatFragment  {
    private static final int RECORDS = 20;
    private RxPermissions rxPermissions;

    /**
     * if it is chatBot
     */
    private boolean isRobot;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rxPermissions = new RxPermissions(getActivity());

        setChatFragmentHelper(new EaseChatFragmentHelper() {
            @Override
            public void onSetMessageAttributes(EMMessage message) {
                if (isRobot) {
                    // 设置消息扩展属性
                    message.setAttribute("em_robot_message", isRobot);
                }

                // 设置消息的扩展属性，携带昵称头像
                Map<String, Object> ext = message.ext();
                Set<Map.Entry<String, Object>> entries = ext.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                while(iterator.hasNext()){
                    Map.Entry<String, Object> next = iterator.next();
                    String value = String.valueOf(next.getValue());
                    Logger.d(TAG+"ChatFragment value : " + value);

                }
                Logger.d(TAG+"ChatFragment value : " + message.toString());

                UserCacheManager.setMsgExt(message);
            }

            @Override
            public void onEnterToChatDetails() {

            }

            @Override
            public void onAvatarClick(String username) {

            }

            @Override
            public void onAvatarLongClick(String username) {

            }

            @Override
            public boolean onMessageBubbleClick(EMMessage message) {
                return false;
            }

            @Override
            public void onMessageBubbleLongClick(EMMessage message) {

            }

            @Override
            public boolean onExtendMenuItemClick(int itemId, View view) {
                switch (itemId) {
                    case 1:
                        rxPermissions
                                .request(Manifest.permission.CAMERA)
                                .subscribe(granted -> {
                                    if (granted) { // Always true pre-M
                                        // I can control the camera now
                                        selectPicFromCamera();
                                    } else {
                                    }
                                });
                        break;
                    case 2:
                        selectPicFromLocal();
                        break;


                }


                return true;
            }

            @Override
            public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
                return null;
            }
        });
    }



}
